package com.css.fxfzypg.modules.rating.library.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.dict.service.DictItemService;
import com.css.fxfzypg.base.dict.viewobjects.DictEntityVo;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.rating.library.model.dto.KpExamScoringDbInsertIndexDto;
import com.css.fxfzypg.modules.rating.library.model.dto.KpUpdatePtgxTypeDto;
import com.css.fxfzypg.modules.rating.library.model.entity.KpExamScoringDb;
import com.css.fxfzypg.modules.rating.library.model.vo.GetIndexListByKesdIdVo;
import com.css.fxfzypg.modules.rating.library.model.vo.KpExamScoringDbListVo;
import com.css.fxfzypg.modules.rating.library.service.KpExamScoringDbService;
import com.css.fxfzypg.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 考评库管理接口
 */
@Slf4j
@RequestMapping("kpExamScoringDb")
@RestController
public class KpExamScoringDbController {

    @Autowired
    private KpExamScoringDbService kpExamScoringDbService;
    @Autowired
    private DictItemService dictItemService;

    /**
     * 应用指标接口
     *
     * @return
     */
    @PostMapping("applyIndex")
    public RestResponse<KpExamScoringDbInsertIndexDto> applyIndex(@RequestBody KpExamScoringDbInsertIndexDto dto) {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            return RestResponse.succeed(kpExamScoringDbService.applyIndex(dto, currUid));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 删除指标接口
     *
     * @param index3Id
     * @return
     */
    @GetMapping("deleteIndexById")
    public RestResponse<String> deleteIndexById(@RequestParam String index3Id) {
        try {
            kpExamScoringDbService.deleteIndexById(index3Id);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 查询考评库列表接口
     *
     * @param param
     * @return
     */
    @GetMapping("list")
    public RestResponse<List<KpExamScoringDbListVo>> list(KpExamScoringDb param) {
        try {
            return RestResponse.succeed(kpExamScoringDbService.list(param));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 根据考评库id查询指标列表
     *
     * @param kesdId 考评库id
     * @return
     */
    @GetMapping("listIndexByKesdId")
    public RestResponse<List<GetIndexListByKesdIdVo>> listIndexByKesdId(@RequestParam String kesdId) {
        try {
            return RestResponse.succeed(kpExamScoringDbService.listIndexByKesdId(kesdId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 删除考评库
     *
     * @return
     */
    @GetMapping("deleteKpk")
    public RestResponse<String> deleteById(String kesdId) {
        try {
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            kpExamScoringDbService.deleteById(kesdId, currUid);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 查询平台更新数据分类
     *
     * @return
     */
    @GetMapping("getPtgxType")
    public RestResponse<List<DictEntityVo>> getPtgxType() {
        try {
            List<DictEntityVo> kpptsjgxzbdflx = dictItemService.getDictItemByDictId("kpptsjgxzbdflx");
            return RestResponse.succeed(kpptsjgxzbdflx);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

    /**
     * 修改指标的平台更新类型
     *
     * @param dto
     * @return
     */
    @PostMapping("updatePtgxType")
    public RestResponse<String> updatePtgxType(@RequestBody KpUpdatePtgxTypeDto dto) {
        try {
            log.debug("param:{}", JSONObject.toJSONString(dto));
            String currUid = PlatformSessionContext.getUserID(); // 获取当前登录用户
            if (currUid == null) {
                return RestResponse.fail("请先登录");
            }
            kpExamScoringDbService.updatePtgxType(dto, currUid);
            return RestResponse.succeed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return RestResponse.fail(e.getMessage());
        }
    }

}
